<template>
  <el-drawer class="dia-style" :title="title" :visible.sync="isOpen" width="60%" :close-on-click-modal="false" size="50%" :wrapper-closable="false">
   <div style="flex:1;">
     <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" @submit.native.prevent>
      <el-form-item label="名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入名称"
          clearable
          style="width: 240px"
          maxlength="20"
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          :header-cell-style="{ background: '#F3F5FC' }"
          @click="handleQuery"
        >查询
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="tableRef"
      v-loading="loading"
      :data="tableList"
      :selection="isMultiple"
      style="height: calc(100% - 160px); overflow-y: auto"
      highlight-current-row
      @selection-change="selectionChange"
      @row-click="singleElection"
    >
      <el-table-column v-if="isMultiple" type="selection" width="55" />
      <el-table-column v-else align="center" width="55" label="选择">
        <template slot-scope="scope">
          <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
          <el-radio v-model="templateSelection" class="radio" :label="scope.row.id">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="120" :index="indexMethod" />
      <el-table-column label="产品名称" prop="productName" :show-overflow-tooltip="true" />
      <el-table-column label="产品类型" prop="productType" :show-overflow-tooltip="true" />
      <el-table-column label="接入厂商" prop="companyName" :show-overflow-tooltip="true" />
      <el-table-column label="产品版本" prop="version" width="140" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="handlePagination"
    />
   </div>
    <div class="el-drawer-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-drawer>
</template>
<script>
import { productList } from '@/api/milkyway/gatherData'

export default {
  props: {
    title: {
      type: String,
      default() {
        return '编辑'
      }
    },
    formData: {
      type: Object,
      default() {
        return {}
      }
    },
    checkData: {
      type: Array,
      default() {
        return []
      }
    },
    size: {
      type: Number,
      default: 10
    },
    isMultiple: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      loading: false,
      total: 0,
      tableList: [],
      checkNodes: [],
      queryParams: {
        productName: '',
        current: 1,
        size: this.size
      },
      isOpen: this.open,
      templateSelection: ''
    }
  },
  mounted() {
    this.getList()
  },

  methods: {
    initSelect(row) {
      this.$nextTick(() => {
        if (row) {
          this.tableList.forEach((item) => {
            row.forEach((i) => {
              if (i.id === item.id) {
                this.$refs.tableRef.toggleRowSelection(item)
              }
            })
          })
        }
      })
    },
    indexMethod(index) {
      if (!this.queryParams.current || !this.queryParams.size) {
        return index + 1
      } else {
        return index + (this.queryParams.current - 1) * this.queryParams.size + 1
      }
    },
    show(data) {
      this.isOpen = true
      this.queryParams.productName = ''
      this.queryParams.current = 1
      this.getList()
      this.$nextTick(() => {
        if (this.isMultiple) {
          this.checkNodes = data
          this.initSelect(this.checkNodes)
        } else {
          if (data.length > 0) {
            this.singleElection(data[0])
          } else {
            this.templateSelection = []
          }
        }
      })
    },
    reset() {
      this.$nextTick(() => {
        // this.toggleSelection()
        this.templateSelection = []
        this.resetForm('queryForm')
      })
    },
    /** 获取产品列表*/
    getList() {
      this.loading = true
      this.queryParams.scrollToTop = true
      productList(this.queryParams).then((res) => {
        this.loading = false
        this.tableList = res.list
        this.total = res.total
        this.loading = false
        this.queryParams.scrollToTop = false
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      if (this.checkNodes.length > 0) {
        // this.$modal.msgSuccess('修改成功')
      }
      this.$emit('handleSubmit', this.checkNodes)
      this.isOpen = false
      this.initSelect(this.checkNodes)
      // this.reset();
    },
    // 取消按钮
    cancel() {
      this.isOpen = false
      this.reset()
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.loading = true
      this.getList()
    },
    /** 勾选表格*/
    selectionChange(w, node) {
      if (this.isMultiple) {
        this.checkNodes = w
      }
    },
    /** 单选按钮事件 */
    singleElection(row) {
      this.templateSelection = row.id
      this.checkNodes = this.tableList.filter((item) => item.id === row.id)
    },
    /** 选中数据*/
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.tableRef.toggleRowSelection(row)
        })
      } else {
        this.$refs.tableRef.clearSelection()
      }
    },
    /** 改变分页*/
    handlePagination() {
      console.log(111111111111);
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-table{
  min-height:508px !important;
}
::v-deep .el-dialog__body{
  padding:0 20px 20px !important;
}
.el-cascader {
  width: 100%;
}
::v-deep .el-radio__label {
  display: none !important;
}
</style>
